package com.project.controller.web;


import com.project.common.utils.MyPageHelper;
import com.project.entity.Course;
import com.project.entity.TeacherInfo;
import com.project.myBeans.CourseTeacherDetail;
import com.project.myBeans.R;
import com.project.myBeans.TeacherInfoDetail;
import com.project.service.CourseService;
import com.project.service.CourseTeachService;
import com.project.service.TeacherInfoService;
import com.project.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@EnableAutoConfiguration
@RestController
@RequestMapping(value="/course")
@Api(description="课程接口",value = "course")
public class CourseController {

    @Autowired
    CourseService courseService;

    @Autowired
    TeacherInfoService teacherInfoService;

    @Autowired
    UserService userService;

    @Autowired
    CourseTeachService courseTeachService;

    /**
     * 添加课程
     * wwh 2018/3/21
     * @param course
     * @return
     */
    @ApiOperation("添加课程")
    @PostMapping("/add")
    public R addCourse(@RequestBody Course course){
        try{
            if(courseService.add(course)){
                return R.ok("创建课程成功");
            }else {
                return R.error("创建课程失败，请重试");
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    /**
     * wwh 2018/3/21
     * @param id
     * @return
     */
    @ApiOperation("按照Id查找课程及课程创建者详细信息")
    @GetMapping("/findCourseAndCreatorById/{id}")
    public R findCourseAndCreatorById(@PathVariable Integer id){
        try{
            Course course = courseService.findById(id);
            TeacherInfoDetail teacherInfoDetail = teacherInfoService.findDetailByTeacherId(course.getCreatorid());
            return R.ok().put("course",course).put("teacherInfoDetail",teacherInfoDetail);
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation("按条件查询/模糊搜索")
    @PostMapping("/findCoursesAndCreatorDetailByCondition")
    public R findByCondition(@RequestBody Course course){
        try{
            List<Course> courses = courseService.findByCondition(course);
          //  List<CourseTeacherDetail> courseTeacherDetailList = new ArrayList<>();
//            for(Course course1 : courses){
//                CourseTeacherDetail courseTeacherDetail = new CourseTeacherDetail();
//                courseTeacherDetail.setCourse(course1);
//                courseTeacherDetail.setTeacherInfoDetail(teacherInfoService.findDetailByTeacherId(course1.getCreatorid()));
//                courseTeacherDetailList.add(courseTeacherDetail);
//            }

            return R.ok().put("data",courses).put("total", MyPageHelper.getTotal(courses));
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    /**
     * Tate 2018/5/29
     *
     * @param name
     * @return
     */
    @ApiOperation("按关键字查找已有课程")
    @PostMapping("/findCourseBycoursename/{name}")
    public R findCourseBycoursename(@PathVariable String name) {
        try {
            List<Course> list = courseService.selectBykeyword(name);
//            System.out.println(list.size());
            if (!list.isEmpty()) return R.error("已经有类似课程，认领即可!");
            else return R.ok("无类似课程，可创建");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    /**
     * Wqd 2018/6/4
     * @param schoolid
     * @return
     */
    @ApiOperation("按学院id查找已有课程")
    @PostMapping("/findCourseByschoolid/{schoolid}")
    public R findCourseByschoolid(@PathVariable Integer schoolid) {
        try {
            List<TeacherInfo> teacherInfoList = teacherInfoService.findBySchoolId(schoolid);
            List<Course> coursesList = new ArrayList<>();
            for (TeacherInfo teacherInfo:teacherInfoList) {
                coursesList.addAll(courseService.selectByCreatorId(teacherInfo.getId()));
            }
            return R.ok().put("data",coursesList);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

}
